home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / listings / v_13_05 / allison / set2.h < prev    next >
C/C++ Source or Header  |  1995-03-27  |  918b  |  54 lines

  1. // set2.h
  2.  
  3. #include <iostream.h>
  4. #include <vector.h>
  5. #include <algo.h>
  6. #include <iterator.h>
  7.  
  8. template<class T>
  9. class Set
  10. {
  11.     friend ostream & operator<<(ostream &os, const Set<T> &s)
  12.     {
  13.         s.print(os);
  14.         return os;
  15.     }
  16.  
  17. public:
  18.     bool contains(const T &) const;
  19.     void insert(const T &);
  20.     void remove(const T &);
  21.     void print(ostream &) const;
  22.  
  23. private:
  24.     vector<T> elems;
  25. };
  26.  
  27. template<class T>
  28. bool Set<T>::contains(const T & x) const
  29. {
  30.     return find(elems.begin(),elems.end(),x) != elems.end();
  31. }
  32.  
  33. template<class T>
  34. void Set<T>::insert(const T & x)
  35. {
  36.     if (!contains(x))
  37.         elems.push_back(x);     // append
  38. }
  39.  
  40. template<class T>
  41. void Set<T>::remove(const T & x)
  42. {
  43.     elems.erase(find(elems.begin(),elems.end(),x));
  44. }
  45.  
  46. template<class T>
  47. void Set<T>::print(ostream & os) const
  48. {
  49.     os << '{';
  50.     copy(elems.begin(),elems.end(),ostream_iterator<T>(os,"\n"));
  51.     os << '}';
  52. }
  53.  
  54.